﻿
Imports System.Reflection

Public Class urlgenerator
    Public Const defaultServerUrl As String = "http://ava.hzj-jie.net"

    Shared Sub New()
        Dim fis() As FieldInfo = Nothing
        fis = GetType(path._path).GetFields(BindingFlags.Public Or BindingFlags.Static)
        assert(Not fis Is Nothing AndAlso fis.Length() > 0)
        For Each fi As FieldInfo In fis
            assert(strstartwith(Convert.ToString(fi.GetValue(Nothing)), network.pathSeperator))
        Next
    End Sub

    Public Shared Function idcard(ByVal serverUrl As String,
                                  ByVal id As String,
                                  Optional ByVal height As Int64 = npos,
                                  Optional ByVal width As Int64 = npos,
                                  Optional ByVal targetUrl As String = Nothing) As String
        If strendwith(serverUrl, network.pathSeperator) Then
            serverUrl = strleft(serverUrl, strlen(serverUrl) - strlen(network.pathSeperator))
        End If
        serverUrl += path.idcard

        serverUrl = urlUtils.appendQuery(serverUrl, query.id, id)
        If width <> npos AndAlso height <> npos Then
            serverUrl = urlUtils.appendQuery(serverUrl, query.size,
                                             Convert.ToString(height) + size.separator + Convert.ToString(width))
        End If
        If Not isemptystring(targetUrl) Then
            serverUrl = urlUtils.appendQuery(serverUrl, query.targetUrl, targetUrl)
        End If

        Return serverUrl
    End Function

    Public Shared Function idcard(ByVal id As String,
                                  Optional ByVal height As Int64 = npos,
                                  Optional ByVal width As Int64 = npos,
                                  Optional ByVal targetUrl As String = Nothing) As String
        Return idcard(defaultServerUrl, id, height, width, targetUrl)
    End Function

    Public Enum portraitFormat
        proxy
        redirect
    End Enum

    Public Shared Function portrait(ByVal serverUrl As String,
                                    ByVal id As String,
                                    Optional ByVal fmt As portraitFormat = portraitFormat.proxy,
                                    Optional ByVal height As Int64 = npos,
                                    Optional ByVal width As Int64 = npos) As String
        If strendwith(serverUrl, network.pathSeperator) Then
            serverUrl = strleft(serverUrl, strlen(serverUrl) - strlen(network.pathSeperator))
        End If
        serverUrl += path.portrait

        serverUrl = urlUtils.appendQuery(serverUrl, query.id, id)
        If fmt = portraitFormat.redirect Then
            serverUrl = urlUtils.appendQuery(serverUrl, query.format, format.redirect)
        End If
        If width <> npos AndAlso height <> npos Then
            serverUrl = urlUtils.appendQuery(serverUrl, query.size,
                                             Convert.ToString(height) + size.separator + Convert.ToString(width))
        End If

        Return serverUrl
    End Function

    Public Shared Function portrait(ByVal id As String,
                                    Optional ByVal format As portraitFormat = portraitFormat.proxy,
                                    Optional ByVal width As Int64 = npos,
                                    Optional ByVal height As Int64 = npos) As String
        Return portrait(defaultServerUrl, id, format, width, height)
    End Function

    Public Enum textFormat
        plain
        html
        ajax
    End Enum
End Class